﻿
#include <stdio.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include "core.h"

//#include "minigui_api.h"
#include "mini_gui.h"
#include "refactoring_func.h"



static bool bGlbRunFlag = true;
static SYS_MODULE_S *pstGlbMango = NULL;

static void SIGNAL_HandleExit(int s32Signal)
{
	if(s32Signal != SIGPIPE) {
		SLOG(WRN, "Catch exit signal %d\n", s32Signal);
		bGlbRunFlag = false;
	} else {
		SLOG(WRN, "Catch signal SIGPIPE\n");
	}
}



static int MANGO_PLATFORM_Init(SYS_MODULE_S *pstMango)
{
	CHECK_ARG(pstMango);
	CHECK_ARG(pstMango->pstFuncModuleSet);

	if(pstMango->s32FuncModuleNum <= 0) {
		SLOG(ERR, "Invalid pthread num: %d\n", pstMango->s32FuncModuleNum);
		return -1;
	}
	if(pstMango->stFops.SYS_Init == NULL) {
		SLOG(ERR, "Invalid SYS_Init\n");
		return -1;
	}
	if(pstMango->stFops.SYS_Proc == NULL) {
		SLOG(ERR, "Invalid SYS_Proc\n");
		return -1;
	}

	if(pstMango->stFops.SYS_ShutDown == NULL) {
		SLOG(ERR, "Invalid SYS_ShutDown\n");
		return -1;
	}




	pstGlbMango = pstMango;
	return 0;
}

int MiniGUIMain (int argc, const char* argv[])
{

#ifdef _MGRM_PROCESSES
		JoinLayer(NAME_DEF_LAYER , "scrollbar" , 0 , 0);
#endif


	FUNC_MODULE_S *pstFuncModuleSet = NULL;
	int i, s32Ret, s32Sta, s32Priority, s32FuncModuleNum;

	signal(SIGINT, SIGNAL_HandleExit);
	signal(SIGILL, SIGNAL_HandleExit);
	signal(SIGPIPE, SIGNAL_HandleExit);
	signal(SIGTERM, SIGNAL_HandleExit);
	signal(SIGKILL, SIGNAL_HandleExit);


	MANGO_PLATFORM_Init(&stGlbArtanis);


	if(NULL == pstGlbMango) {
		QLOG(ERR, "Failed to initialize the platform of mango !!!\n");
		return -1;
	}

	pstFuncModuleSet = pstGlbMango->pstFuncModuleSet;
	s32FuncModuleNum = pstGlbMango->s32FuncModuleNum;



	for(i = s32Priority = 0; i < s32FuncModuleNum; i++) {
		pstFuncModuleSet[i].u32Id = i;
	}


	/*- Init pthread module -*/
	for(i = 0; i < s32FuncModuleNum; i++) {
		if(pstFuncModuleSet[i].stFops.FMODULE_Init) {
			s32Ret = pstFuncModuleSet[i].stFops.FMODULE_Init(pstFuncModuleSet[i].pArg);
			if(s32Ret) QLOG(ERR, "The pthread_module[%s] init failed\n", pstFuncModuleSet[i].s8Name);
		}
	}

	/*- Create running pthread -*/
	for(i = s32Sta = 0; i < s32FuncModuleNum; i++) {
		if(pstFuncModuleSet[i].stFops.FMODULE_Pthread) {
			s32Ret = pthread_create(&pstFuncModuleSet[i].stPid, NULL,
					pstFuncModuleSet[i].stFops.FMODULE_Pthread, &pstFuncModuleSet[i]);
			if(s32Ret != 0) {
				QLOG(ERR, "pthread_create[%s] failed, %s\n", pstFuncModuleSet[i].s8Name, strerror(errno));
			} else {
				//SLOG(PRI, "pthread_create: %s success\n", pstFuncModuleSet[i].s8Name);
				s32Sta |= (1<<pstFuncModuleSet[i].u32Id);
			}
		}
	}

	while(bGlbRunFlag) {
		sleep(1);
	}


	/*- Deinit pthread module -*/
	for(i = s32FuncModuleNum - 1; i >= 0; i--) {
		if(pstFuncModuleSet[i].stFops.FMODULE_DeInit) {
			s32Ret = pstFuncModuleSet[i].stFops.FMODULE_DeInit(pstFuncModuleSet[i].pArg);
			if(s32Ret) QLOG(ERR, "pthread module: %s deinit failed\n", pstFuncModuleSet[i].s8Name);
				else
				QLOG(PRI, "pthread module: %s deinit success\n", pstFuncModuleSet[i].s8Name);
		}
	}

	for(i = s32FuncModuleNum - 1; i >= 0; i--) {
		if((s32Sta >> pstFuncModuleSet[i].u32Id) & 0x01) {
			QLOG(PRI, "pthread_join: %s\n", pstFuncModuleSet[i].s8Name);
			pthread_join(pstFuncModuleSet[i].stPid, NULL);
		}
	}
	_system("killall -9 minigui", 5);
	return 0;
}

#ifdef _MGRM_THREADS
#include <minigui/dti.c>
#endif






